import UserImformation.Account;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import java.io.*;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String user_name = req.getParameter("user_name");
        String password1 = req.getParameter("password1");
        String msg = null;
        String str = null;
        msg = user_name + "," + password1;
        int exit = 0;

        File file = new File("UserInfo.txt");

        //判断用户名是否已经存在
        int n = 0;
        Reader reader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(reader);
        while ((str = bufferedReader.readLine()) != null) {
            if (!str.trim().equals("")) {
                n++;
            }
        }
        bufferedReader.close();
        reader.close();
        int len = 0;
        Account[] users;

        if (n != 0) {
            users = new Account[n];
            Jsonb jsonb = JsonbBuilder.create();
            reader = new FileReader(file);
            bufferedReader = new BufferedReader(reader);
            while ((str = bufferedReader.readLine()) != null) {
                if (!str.trim().equals("")) {
                    users[len++] = jsonb.fromJson(str, Account.class);
                }
            }
            bufferedReader.close();
            reader.close();

            if (users != null) {
                for (int i = 0; i < users.length; i++) {
                    if (users[i].getUsername().equals(user_name)) {
                        exit = 1;
                        break;
                    }
                }
            }
        }
        if (exit == 0) {
            //写入
            BufferedWriter out = null;
            Account account = new Account(user_name, null, null, null, null, null, null, null, "One_Piece.png", password1);
            try {
                Jsonb jsonb = JsonbBuilder.create();
                jsonb = JsonbBuilder.create();
                String accountJson = jsonb.toJson(account);
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
                out.write(accountJson + "\n");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        String json = "{\"exit\":\"" + exit + "\"}";
        resp.setContentType("application/json, charset = utf-8");
        resp.getWriter().println(json);
    }
}
